package com.baijiayun.live.ui;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.baijiayun.live.ui.activity.LiveRoomBaseActivity;
import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.livecore.models.LPJoinCodeEnterRoomModel;
import com.baijiayun.livecore.models.LPSignEnterRoomModel;
import com.baijiayun.liveuibase.listeners.share.LPShareListener;
import com.baijiayun.liveuibase.widgets.dialog.ThemeMaterialDialogBuilder;

/**
 * 入口类
 * Created by Shubo on 2017/2/13.
 */

public class LiveSDKWithUI {
    /**
     * 后台音频开关
     */
    public static boolean isAudioBackOpen = true;

    public static void enterRoomWithOne2OneTemplate(@NonNull Context context, LPJoinCodeEnterRoomModel joinCodeEnterRoomModel) {
        enterRoom(context, joinCodeEnterRoomModel, 1);
    }

    public static void enterRoomWithOne2OneTemplate(@NonNull Context context, LPSignEnterRoomModel signEnterRoomModel) {
        enterRoom(context, signEnterRoomModel, 1);
    }

    public static void enterRoom(@NonNull Context context, LPJoinCodeEnterRoomModel joinCodeEnterRoomModel) {
        enterRoom(context, joinCodeEnterRoomModel, 2);
    }

    public static void enterRoom(@NonNull Context context, LPSignEnterRoomModel signEnterRoomModel) {
        enterRoom(context, signEnterRoomModel, 2);
    }

    /**
     * 参加码进房间
     * @param context
     * @param joinCodeEnterRoomModel
     * @param templateType
     */
    private static void enterRoom(@NonNull Context context, LPJoinCodeEnterRoomModel joinCodeEnterRoomModel, int templateType){
        addDefaultLoginConflictCallback(context);
        Class clazz;
        if (templateType == 1) {
            clazz = LiveRoomSingleActivity.class;
        } else {
            clazz = LiveRoomTripleActivity.class;
        }
        Intent intent = new Intent(context, clazz);
        intent.putExtra("code", joinCodeEnterRoomModel);
        context.startActivity(intent);
    }

    /**
     * 签名进房间
     * @param context
     * @param signEnterRoomModel
     * @param templateType
     */
    private static void enterRoom(@NonNull Context context, LPSignEnterRoomModel signEnterRoomModel, int templateType){
        //设置直播间登录冲突回调
        addDefaultLoginConflictCallback(context);

        Class clazz;
        if (templateType == 1) {
            clazz = LiveRoomSingleActivity.class;
        } else {
            clazz = LiveRoomTripleActivity.class;
        }
        Intent intent = new Intent(context, clazz);
        intent.putExtra("sign", signEnterRoomModel);
        context.startActivity(intent);
    }

    public static void setRoomExitListener(LPRoomExitListener listener) {
        LiveRoomBaseActivity.setRoomExitListener(listener);
    }

    public static void setShareListener(LPShareListener listener) {
        LiveRoomBaseActivity.setShareListener(listener);
    }

    public static void setEnterRoomConflictListener(RoomEnterConflictListener listener) {
        LiveRoomBaseActivity.setEnterRoomConflictListener(listener);
    }

    public static void setRoomLifeCycleListener(LPRoomResumeListener listener) {
        LiveRoomBaseActivity.setRoomLifeCycleListener(listener);
    }

    public static void disableSpeakQueuePlaceholder() {
        LiveRoomBaseActivity.disableSpeakQueuePlaceholder();
    }

    /**
     * 跑马灯字段
     */
    public static void setLiveRoomMarqueeTape(String str) {
        LiveRoomBaseActivity.setLiveRoomMarqueeTape(str);
    }

    public static void setLiveRoomMarqueeTape(String str, int interval) {
        LiveRoomBaseActivity.setLiveRoomMarqueeTape(str, interval);
    }

    public interface LPRoomResumeListener {
        void onResume(Context context, LPRoomChangeRoomListener listener);
    }

    public interface LPRoomChangeRoomListener {
        void changeRoom(String code, String nickName);
    }

    public interface RoomEnterConflictListener {
        void onConflict(Context context, LPConstants.LPEndType endType, LPRoomExitCallback callback);
    }

    public interface LPRoomExitListener {
        void onRoomExit(Context context, LPRoomExitCallback callback);
    }

    public interface LPRoomExitCallback {
        void exit();

        void cancel();
    }

    /**
     * 登录冲突默认弹框提示
     */
    private static void addDefaultLoginConflictCallback(Context context){
        LiveSDKWithUI.setEnterRoomConflictListener((context12, endType, callback) -> {
            String message;
            switch (endType) {
                case iOS:
                    message = "iOS";
                    break;
                case PC_H5:
                case PC_HTML:
                    message = "PC网页";
                    break;
                case PC_Client:
                    message = "PC客户";
                    break;
                case PC_MAC_Client:
                    message = "MAC客户";
                    break;
                case Android:
                    message = "Android";
                    break;
                default:
                    message = endType.name();
                    break;
            }
            new ThemeMaterialDialogBuilder(context12)
                    .content(String.format(context.getString(R.string.live_login_conflict), message))
                    .positiveText(R.string.confirm)
                    .canceledOnTouchOutside(false)
                    .onPositive((dialog, which) -> callback.exit())
                    .show();
        });
    }
}
